function cl(u,i,t,s) {if(document.images){var z="";if(u.href)z="&url="+encodeURI(u.href);new Image().src="/click.php?table="+encodeURI(t)+"&id="+encodeURI(i)+z+"&sig="+s;}else{u.href="/click.php?table="+encodeURI(t)+"&id="+encodeURI(i)+"&url="+encodeURI(u.href)+"&sig="+s;u.onmousedown="";}return true;}
function FlashRequest() {alert('请点击左下方的播放按钮');}
function needHelp(img,txt) {var helptxt=encodeURIComponent(txt?txt:'有问题？请联系在线客服');document.write('<scr' + 'ipt type="text/javascript" src="http://loopo.cn/livehelp/js/status_image.php?base_url=http://loopo.cn/livehelp&amp;l=avenger&amp;x=1&amp;deptid=1&amp;text=' + helptxt + '"></scr' + 'ipt>');}
function imgcopy(evt) {evt = ( evt ) ? evt : ((window.event) ? event : null);if (typeof evt.button != 'undefined' && evt.button > 1) {alert('请尊重乐铺的版权！谢谢！');}}

var LooPo = {
    initialize: function() {
        this.timer = null;
        this.f = null;
        this.t = 'success';
        this.i = '未知错误';
        this.debug = 1; //1 for firebug, 2 for ajax
        this.aa = new cpaint();this.aa.set_transfer_mode('POST');this.aa.set_debug(this.debug==2);this.aa.set_response_type('JSON');
    },

    shopcar_edit: function() {
        this.f = $('updateform');
        if(this.f.emptycar.value == 1 &&!confirm('您确认不需要这些物品了吗？')) {
            this.f.emptycar.value = 0;
            return false;
        }
        try {
            this.aa.call('/shopcar.php', 'ajaxShopcarUpdate', this.shopcaredit_result, this.f.serialize());
            this.f.disable();
            return false;
        }
        catch(e) {
            if (LooPo.debug) console.debug(e);
            return true;
        }
    },
    
    shopcaredit_result: function(result) {
        var id;
        var d = $('updatemsg');
        this.f = $('updateform');
        
        try {
            stat = result.result[0].data;
            if(stat == 1 || stat == 8) {
                this.i = '购物车已更新! ';
                if(stat == 8) {
                    this.i += '某些物品数量超过库存，我们自动为您调整了数量。<a href="/faq/24" target="_faq">详情</span>';
                    LooPo.t = 'notice';
                }
                if(!result.price) return LooPo._emptycar();
                var dib = [];
                for(var j = 0; j < result.price.length; j++) {
                    id = result.price[j].id[0].data;
                    $('price' + id).update(result.price[j].price[0].data);
                    $('amount' + id).value = result.price[j].amount[0].data;
                    if(result.price[j].amount[0].data == 0) dib.push(id);
                }
                
                $('totalprice').update(result.total[0].data);
                $('cartotalprice').update(Math.round(result.total[0].data*100)/100);
                $('caramount_em').update(result.totalamount[0].data);
                $('caramount').update(result.totalamount[0].data);
                $('totalamount').update(result.totalamount[0].data);

                if(!result.totalamount[0].data) return LooPo._emptycar();
            }
            if(stat == 2) {
                this.i = '<b>更新失败</b>，请检查您的输入后再提交';
                LooPo.t = 'error';
            }
        }
        catch(e) {
            if (LooPo.debug) console.debug(e,result);
            if(this.f.emptycar.value == 1) {
                return LooPo._emptycar();
                location.reload();
                this.f.hide();
                this.i = '加载中';
                var emptycar = 1;
            }
            else {
                this.i = '服务器错误，请稍候再试';
                LooPo.t = 'error';
            }
        }
        if(emptycar == 1) {
            location.href = "/shopcar/checkout";
        }
        else {
            try {
                this.f.enable();
                dib.each(function(id) {
                    new Effect.Fade($('amount'+id).up(1),{duration:.5});
                    //$('amount'+id).up(1).hide();
                });
                d.show();
                if(i) d.update('<div class="'+ LooPo.t +'">' + this.i + '</div>');
                new Effect.ScrollTo('top');
                if(this.timer) window.clearTimeout(this.timer);
                this.timer = setTimeout("$('updatemsg').hide();", 20000);
            }
            catch(e) {
                if (LooPo.debug) console.debug(e,result);
            }
        }
    },

    _emptycar: function() {
        try {
            $('cartext').update('<span id="cartext">您的购物车还是空的<br />继续加油哦！</span>');
            $('icon_shopcar').addClassName('icon_shopcar_empty');
            $('icon_shopcar').removeClassName('icon_shopcar_full');
            $('shopcar_gc').removeClassName('sprites icon_bubble1');
            $('caramount_em').update();
            $('updatemsg').hide();
            $('shopcar_c').update('<div class="error">您的购物车中没有任何物品，为何不看看我们的 <a href="/whathot">热门物品</a>?</div>');
        }
        catch(e) {
            if (LooPo.debug) console.debug(e);
        }
    },

    checkout_submit: function() {
        var f = $('checkoutform');
        try {
            this.aa.call('/checkout.php', 'ajaxCheckout', this.checkout_result, this.f.serialize());
            this.f.disable();
            return false;
        }
        catch(e) {
            if (LooPo.debug) console.debug(e);
            return true;
        }
    },

    checkout_result: function(result) {
        var d = $('updatemsg');

        try {
            var stat = result.result[0].data;
            if(stat != 1 && stat != 2 && stat != 4 && stat != 8) {
                location.reload();
                return false;
            }
            if(stat == 1) {
                this.i = '订单已提交成功，请稍候';
                location.href = '/pay.php/' + result.indentid[0].data + '/1';
                return false;
            }
            if(stat == 2) this.i = '<b>更新失败</b>，请检查您的输入后再提交';
            if(stat == 4) {
                this.i = '为保证物品能及时快捷的到达您的收中，请填写完整您的收货信息，谢谢!';
                LooPo.t = 'error';
            }
            if(stat == 8) {
                alert('注意：由于某些物品库存不够，我们调整了您的订单。请再次确认！');
                location.reload();
            }
        }
        catch(e) {
            if (LooPo.debug) console.debug(e,result);
            this.i = '服务器错误，请稍候再试';
            LooPo.t = 'error';
        }
        $('checkoutform').enable();
        if(i) {
            d.update('<div class="'+ LooPo.t +'">' + this.i + '</div>');
            $('updatemsg_div').show();
            new Effect.ScrollTo('top');
            if(this.timer) window.clearTimeout(this.timer);
            this.timer = setTimeout("$('updatemsg_div').hide();", 20000);
        }
    },

    change_voucher: function() {
        try {
            $('voucher_btn_sub').disabled = true;
            this.aa.call('/voucher.php', 'ajaxGetVoucher', this.change_voucher_result, 'str=' + $F('voucher_code') + '&price=' + totalprice);
        }
        catch(e) {
            if (LooPo.debug) console.debug(e);
            alert('服务器错误，请稍候再试');
        }
        return false;
    },

    change_voucher_result: function(result) {
        $('voucher_btn_sub').disabled = false;
        var voucher_price = parseFloat(result.price[0].data);
        var error = parseInt(result.error[0].data);
        var errmsg = '代码无效';
        if(error > 1) {
            if(error == 4) errmsg = '您的购物金额至少要为 &yen;' + voucher_price;
            voucher_price = 0;
            $('voucher_btn', 'voucher_area').invoke('toggle');
        }
        $('voucher_price').update(voucher_price ? '&yen; - ' + voucher_price : errmsg);
        if(voucher_price) {
            var p = Math.round((totalprice-voucher_price)*100)/100;
            $('totalprice').update(p>0?p:0);
            $('sumprice').update($('totalprice').innerHTML);
        }
        $('voucher_code_h').value = $F('voucher_code');
        //LooPo.change_sumprice(parseFloat($('shipfee').innerHTML));
    },

    change_sumprice: function(postprice) {
        if(!postprice) {
            var radioGrp = $('checkoutform').postfee;
            for(var i = 0; i < radioGrp.length; i++) {
                if(radioGrp[i].checked) var postprice = parseInt(radioGrp[i].value);
            }
        }
        var sumprice = Math.round((parseFloat($('totalprice').innerHTML)+postprice)*100)/100;
        $('sumprice').update((parseInt(sumprice) > 0 ? sumprice : 0));
    },

    animatePoof: function(imageToPoof) {
        poofCount++;
        if(poofCount >= 6) {
            $('imageToPoof').src = "/images/blank.gif";
            clearInterval(poofTimer);
        } else {
            $('imageToPoof').src = eval("poof" + poofCount + ".src");
        }
    },

    alertme: function(id) {
        window.open('/alertme.php/' + id + '/pop/', '_alertme', 'width=300,height=200');
        return true;
    },

    addtocar: function(id, msg) {
        if(msg == null) {
            msg = true;
            showmsg = msg;
        }
        this.aa.call('/addtocar.php', 'ajaxAddToCar', this.addtocar_result, 'id=' + id);
        return false;
    },

    addtocar_result: function(result) {
        var d = $('updatemsg');

        try {
            var stat = result.result[0].data;
            var c = result.count[0].data;
            var p = Math.round(result.totalprice[0].data*100)/100;

            if(stat == 1) this.i = '成功了！您目前共有 <b>' + c + '</b> 件战利品。点击右上角的按钮可以快速结帐!';
            if(stat == 2) this.i = '失败了，没有找到您要加入的物品!';
            if(stat == 8) this.i = '乐铺提醒您：您购物车中的数量已超过该物品库存，您不能够购买更多。<a title="通过我们的帮助系统了解详情" href="/faq.php/23" target="_blank" class="white">详情<\/a>';
            if(stat == 4) this.i = '很抱歉，我们的网站有些问题，您可以稍候再试一次!';
            if(stat == 2 || stat == 4 || stat == 8) {
                LooPo.t = 'error';
            } else {
                //动画效果
                /*
                poofCount = 0;
                poofTimer = setInterval("LooPo.animatePoof('car_eff')", 75);

                $('promo').show();
                */
            }
        }
        catch(e) {
            if (LooPo.debug) console.debug(e,result);
            this.i = '很抱歉，我们的网站有些问题，您可以稍候再试一次';
            LooPo.t = 'error';
        }
        if(stat == 1) {
            $('cartext').update('<a href="/shopcar">您共有 <b id="caramount">' + c + '<\/b> 件战利品!<br />总价 <b id="cartotalprice">&yen;'+ p +'</b> 元</b></a>');
            $('caramount_em').update(c);
            $('icon_shopcar').addClassName('icon_shopcar_full');
            $('shopcar_gc').addClassName('sprites icon_bubble1');
        }
        d.update('<div class="'+ LooPo.t +'">' + this.i + '</div>');
        d.show();
        if(this.timer) window.clearTimeout(this.timer);
        this.timer = window.setTimeout("$('updatemsg').hide()", 20000);
    },

    addtoWishlist: function(remove,id) {
        LooPo.iid = id;
        try {
            if (remove)
                this.aa.call('/wishlist.php/add/'+id, 'ajaxnewWishList', this.addtoWishlist_result, 'remove=1&iid='+id);
            else
                this.aa.call('/wishlist.php/add/'+id, 'ajaxnewWishList', this.addtoWishlist_result, 'iid='+id);
            return false;
        } catch (e) {
            if (LooPo.debug) console.debug(e);
            return true;;
        }
    },

    addtoWishlist_result: function(result) {
        var d = $('updatemsg');

        try {
            var stat = result.result[0].data;
            switch(stat) {
                case 1:
                case 4:
                    this.i = '该物品已加入您的 Wishlist，您可以在会员中心进行管理';
                    $('addtowishlist').update('<a style="font-weight:bold" onclick="javascript:return LooPo.addtoWishlist(1,'+ LooPo.iid +');" href="/wishlist/add/'+ LooPo.iid +'?remove=1">从我的 Wishlist 中移除</a>');
                    break;
                case 8:
                    LooPo.t = 'notice';
                    this.i = '该物品已从您的 Wishlist 中移除';
                    $('addtowishlist').update('<a onclick="javascript:return LooPo.addtoWishlist(0,'+ LooPo.iid +');" href="/wishlist/add/'+ LooPo.iid +'">加入我的 Wishlist</a>');
                    break;
                default:
                    LooPo.t = 'error';
                    this.i = '未知错误('+stat+')，请稍候再试';
            }
        } catch(e) {
            if (LooPo.debug) console.debug(e,result);
            this.i = '服务器错误，请稍候再试';
            LooPo.t = 'error';
        }

        if (i) {
            d.update('<div class="'+ LooPo.t +'">' + this.i + '</div>'); d.show();
            if (this.timer) window.clearTimeout(this.timer);
            this.timer = setTimeout( "$('updatemsg').hide();", 10000);
        }
    },

    newComment: function(id) {
        this.f = $('newcommentform');
        $('submit').disabled = true;

        try {
            this.aa.call('/comments.php/'+ id +'/new', 'ajaxNewComment', this.newComment_result, 'iid='+ id + '&' + this.f.serialize());
            return false;
        } catch (e) {
            if (LooPo.debug) console.debug(e);
            return true;
        }
    },

    newComment_result: function(result) {
        var err = 0;
        var ok = 0;
        var d = $('updatemsg2');

        try {
            var stat = result.result[0].data;
            switch(stat) {
                //case 32:    this.i = 'Email貌似不正确哦';err = 1;$('umail').focus();break;
                case 16:    this.i = '姓名不能为空哟';err = 1;$('uname').focus();break;
                case 8:     this.i = '老兄，你想说什么呀';err = 1;$('body').focus();break;
                case 128:   this.i = '不要这么小气啊，再说点吧';err = 1;$('body').focus();break;
                case 4:     this.i = '相同的话说一遍就行了喔';err = 1;$('body').focus();break;
                case 1:
                        var node = result.cdata[0];
                        var newhtml = '<div class="span-19" style="background:#ebe8dd;padding:4px;margin:4px 0;border:1px solid #EEEEEE">';
                        newhtml += '<div class="span-2"><img alt="" width="32" height="32" src="/images/icon/no-gravatar.gif" align="left" hspace="5" /></div>';
                        newhtml += '<div class="span-17 last">';
                        newhtml += '<b class="default">'+ node.uname[0].data +'</b>@'+ node.updated[0].data +'<br />';
                        newhtml += '<blockquote style="margin:0.6em">' + node.body[0].data + '</blockquote></div></div>';

                        $('items_comments_list').insert({top:newhtml});
                        ok = 1;$('rand_code_img').src = '/rand_code.php?'+Math.random();
                        new Effect.ScrollTo('items_comments_list');
                        $('newcommentform').reset();
                        break;
                case 256:
                        this.i = '您输入的验证数字不正确,或者您的留言中不包括中文，请检查';err = 1;try{$('rand_code').focus()}catch(e){};break;
                default:
                        err = 1; this.i = '未知错误('+stat+')，请稍候再试';
            }
        } catch(e) {
            if (LooPo.debug) console.debug(e,result);
            this.i = '服务器错误，请稍候再试';
            err = 1;
        }

        $('submit').disabled = false;
        if (i) {
            LooPo.t = (err==1 ? 'error' : 'notice');
            d.update('<div class="'+ LooPo.t +'">' + this.i + '</div>');
            d.show();
            if (this.timer) window.clearTimeout(this.timer);
            this.timer = setTimeout( "$('updatemsg2').hide();", 20000);
        }
        if (ok) d.hide();
    },

    user_edit: function() {
        $('submit').disabled = true; $('submit1').disabled = true;
        this.f = $('updateform');
        try {
            this.aa.call('/myRegInfo.php', 'ajaxuserRegInfo', this.useredit_result, this.f.serialize());
            return false;
        } catch (e) {
            if (LooPo.debug) console.debug(e);
            return true;
        }
    },

    useredit_result: function(result) {
        var d = $('updatemsg');
        try {
            var stat = result.result[0].data;
            if (stat==1)    this.i = '您的注册资料已更新';
            if (stat==2)    this.i = '更新失败，请检查您填写的资料完整性后再提交';
            if (stat&8)     this.i = '更新失败，请检查您的邮箱地址';
            if (stat&4)     this.i = '更新失败，请检查您的密码长度';
            if (stat&16)    this.i = '输入的密码与确认密码不符';
        } catch(e) {
            if (LooPo.debug) console.debug(e,result);
            var err = 1;
            this.i = '服务器错误，请稍候再试';
        }

        if (stat&8 || stat&4 || stat&16 || err) {
            LooPo.t = 'error';
        }

        $('submit').disabled = false; $('submit1').disabled = false;
        d.update('<div class="'+ LooPo.t +'">' + this.i + '</div>');
        d.show();
        
        if (this.timer) window.clearTimeout(this.timer);
        this.timer = window.setTimeout( "$('updatemsg').hide();", 10000);

        if (stat&4)     $('password').select();
        if (stat&8)     $('email').select();
        if (stat&16)    $('password_r').select();
    },

    paddress_change: function() {
        try{
            $('submit').disabled = true;
            $('submita').disabled = true;
        } catch(e) {}
        this.f = $('updateform');
        this.aa.call('/myAddress.php', 'ajaxuserAddressPrimarySet', this.paddress_changed, this.f.serialize());
        return false;
    },

    paddress_changed: function(result) {
        var d = $('updatemsg');
        try {
            var stat = result.result[0].data;
            if (stat==1) this.i = '默认收货地址已更新';
            if (stat==2) {
                this.i = '更新失败，请检查您的选择后再试';
                LooPo.t = 'error';
            }
        } catch(e) {
            this.i = '服务器错误，请稍候再试';
            LooPo.t = 'error';
        }

        try{$('err4').hide()} catch(e) {}

        try{
            $('submit').disabled = false;
            $('submita').disabled = false;
        } catch(e) {}
        d.update('<div class="'+ LooPo.t +'">' + this.i + '</div>');
        d.show();
        if (this.timer) window.clearTimeout(this.timer);
        this.timer = setTimeout( "$('updatemsg').hide();", 10000);
    },

    wishaddress: function() {
        $('submit').disabled = true;
        this.f = $('updateform');
        $('updatemsg').hide();

        try {
            this.aa.call('/myWishList.php', 'ajaxuserWishListAddress', this.wishaddress_result, this.f.serialize());
            return false;
        } catch (e) {
            return true;
        }
    },

    wishaddress_result: function(result) {
        var d = $('updatemsg');
        try {
            var stat = result.result[0].data;
            if (stat==1)    this.i = '您的送达地址已更新';
            if (stat&4)     {
                this.i = '请填写完整的地址信息';
                LooPo.t = 'error';
            }
        } catch(e) {
            this.i = '服务器错误，请稍候再试';
            LooPo.t = 'error';
        }
        
        //地址为空时提示 
        try{$('erraddr').hide();}catch(e){}

        $('submit').disabled = false;
        d.update('<div class="'+ LooPo.t +'">' + this.i + '</div>');
        d.show();
        
        if (this.timer) window.clearTimeout(this.timer);
        this.timer = setTimeout( "$('updatemsg').hide();", 20000);
    },

    removewish: function(id) {
        $('w'+id).remove();
        try {
            $('submit').disabled = true;
            $('submita').disabled = true;
        } catch(e) {}
        
        this.aa.call('/myWishList.php/remove/'+id, 'ajaxuserWishRemove', this.removewish_result, "remove="+id);
        return false;
    },

    removewish_result: function(result) { 
        tojs();
    },

    //客服论坛
    newPost: function(id) {
        $('submit').disabled = true;
        $('updatemsg').hide(); $('loading').show();

        try {
            this.aa.call('/forum.php/'+id+'/new', 'ajaxNewPost', this.newPost_result, $('newtopicform').serialize());
            return false;
        } catch (e) {
            return true;
        }
    },

    newPost_result: function(result) {
        var d = $('updatemsg');
        $('loading').hide();

        try {
            var stat = result.result[0].data;
            if (stat != 1) LooPo.t = 'error';
            switch(stat) {
                case 1:
                    this.i = '发表成功，正转到列表页';break;
                case 4:
                    this.i = '别忘了输入标题哦';$('subject').focus();break;
                case 8:
                    this.i = '您想要说什么呢';$('body').focus();break;
                case 16:
                    this.i = '姓名不能为空哦';$('uname').focus();break;
                case 32:
                    this.i = '请检查您的Email是否正确';$('umail').focus();break;
                case 128:
                    this.i = '朋友，您输入的验证码好像不正确';$('rand_code').focus();break;
                default:
                    this.i = '未知错误('+stat+')，请稍候再试';
            }
        } catch(e) {
            this.i = '服务器错误，请稍候再试';
            LooPo.t = 'error';
        }

        $('submit').disabled = false;
        d.update('<div class="'+ LooPo.t +'">' + this.i + '</div>');
        d.show();
        
        if (this.timer) window.clearTimeout(this.timer);
        this.timer = setTimeout( "$('updatemsg').hide();", 20000);
        if (LooPo.t != 'error') setTimeout( "location.href='/forum/1';", 500);
    },

    newReply: function(id, tid) {
        $('submit').disabled = true;
        $('updatemsg').hide(); $('loading').show();
        
        try {
            this.aa.call('/forum.php/'+id+'/'+tid, 'ajaxNewReply', this.newReply_result, $('replyform').serialize());
            return false;
        } catch (e) {
            return true;
        }
    },

    newReply_result: function(result) {
        var d = $('updatemsg');
        $('loading').hide();

        try {
            var stat = result.result[0].data;
            if (stat != 1) LooPo.t = 'error';
            switch(stat) {
                case 32:
                    this.i = '请检查您的Email是否正确';$('umail').focus();break;
                case 16:
                    this.i = '姓名不能为空哦';$('uname').focus();break;
                case 8:
                    this.i = '您想说什么';$('body').focus();break;
                case 4:
                    this.i = '请不要重复发布相同内容，谢谢';$('body').focus();break;
                case 1:
                    var node = result.fdata[0];
                    var newhtml = '<hr size="1" /><b>'+ node.uname[0].data +'<\/b>';
                    newhtml += ' &nbsp; 在&nbsp; '+ node.updated[0].data +' 回应：<br />';
                    newhtml += node.body[0].data;
                    $('toplist').innerHTML += newhtml;
                    $('body').focus(); $('body').value = '';
                    break;
                default:
                    this.i = '未知错误('+stat+')，请稍候再试';
            }
        } catch(e) {
            this.i = '服务器错误，请稍候再试';
            LooPo.t = 'error';
        }

        $('submit').disabled = false;
        if (this.i) {
            d.update('<div class="'+ LooPo.t +'">' + this.i + '</div>');
            d.show();
            if (this.timer) window.clearTimeout(this.timer);
            this.timer = setTimeout( "$('updatemsg').hide();", 20000);
        }
        if (LooPo.t != 'error') $('updatemsg').hide();
    }
};

function _dg(m, ele) {
    if (typeof ele == 'undefined') ele = $('itemlist_table') || $('itemlist');
    $A(ele.getElementsByTagName('a')).each(function(node) {
        node.onclick = function(){return m;}
    });
}

function __de(ele) {
    window.setTimeout(function(){_dg(true,ele.element)}, 1000);
}

function __dg(ele) {
    return _dg(false, ele.element);
}

LooPo.initialize();
